package com.loongtech.bi.manager.business;

import com.loongtech.bi.entity.pay.EntityFirstPayUser;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddcashBalanceManager extends ManagerQueryCacheBase<EntityFirstPayUser> {
	private static final long serialVersionUID = 1L;

    @Autowired
    private ConfigManager configManager;

	/**
	 * 根据Addtype 查询 总充值
	 * @param dateStart
	 * @param dateEnd
	 * @return
	 */
	public List<Object[]> getSumAddcashByAllKindByAddtype(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer,String addtype) {
        String s_257 = configManager.getConfigValueByName(ConfigManager.S_257);  // 是否特殊处理addtype为257的类型
        if (StringUtils.isNotEmpty(s_257) && "1".equals(s_257)) {
            return findByQuery("select sum(sumcashadd) from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and bitand(addtype," + addtype + ")=" + addtype + " and sumcashadd is not null", "dayStart", dateStart, "dayEnd", dateEnd);
        } else {
            return findByQuery("select sum(sumcashadd) from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and bitand(addtype," + addtype + ")=" + addtype +" and sumcashadd is not null", "dayStart", dateStart, "dayEnd", dateEnd);
        }
    }
	
	/**
	 * 获取订单数总和
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @param queryChannel
	 * @param queryServer
	 * @return
	 */
	public List<Object[]> getSumOrderNumByAllKind(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select sum(outerorderidnum) from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"'", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 获取订单数总和 根据Addtype
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @param queryChannel
	 * @param queryServer
	 * @param addtype
	 * @return
	 */
	public List<Object[]> getSumOrderNumByAllKindByAddtype(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer,String addtype) {
		return findByQuery("select sum(outerorderidnum) from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and bitand(addtype," + addtype + ")=" + addtype +" ", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 根据Addtype 查询EntityAddcashBalance
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @param queryChannel
	 * @param queryServer
	 * @param addtype
	 * @return
	 */
	public List<Object[]> getAddcashByAllKindByAddtypeByday(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer,String addtype) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),sumcashadd from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and bitand(addtype," + addtype + ")=" + addtype +" ", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 获取订单数
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @param queryChannel
	 * @param queryServer
	 * @return
	 */
	public List<Object[]> getOrderNumByAllKindByDay(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),sum(outerorderidnum) from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' group by day", "dayStart", dateStart, "dayEnd", dateEnd);
	}
	
	/**
	 * 获取订单数 根据Addtype
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @param queryChannel
	 * @param queryServer
	 * @param addtype
	 * @return
	 */
	public List<Object[]> getOrderNumByAllKindByAddtypeByDay(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer,String addtype) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),outerorderidnum from EntityAddcashBalance  where 1=1 and day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' and bitand(addtype," + addtype + ")=" + addtype +" ", "dayStart", dateStart, "dayEnd", dateEnd);
	}
}
